//用户路由信息
const express = require('express')
const router = express.Router()
//用户接口信息
const { userregister, userlogin, userinfo, editUser, pass } = require('../Interface/user')
//验证令牌中间件
const verifyjwt = require('../middleware/checkToken')
const multer = require('multer')
const path = require('path')

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, path.join(__dirname, '../', 'uploads'))
  },
  filename: function (req, file, cb) {
    let extname = path.extname(file.originalname)
    let time = +new Date()
    let str = 'abcdefghijklmnopqrstuvwxyz0123456789'
    let random = ''
    for (let a = 0; a < 6; a++) {
      random += str[Math.floor(Math.random() * str.length)]
    }
    // cb(null, 文件名称)
    cb(null, time + random + extname)
  }
})
const upload = multer({ storage: storage })
// 用户信息
//注册
router.post('/register', userregister)

// 登录
router.post('/login', userlogin)

//获取用户信息
// router.get('/info/:id', verifyjwt, userinfo)

//修改用户信息
router.put('/editUser/:_id', verifyjwt, upload.single('avatar'), editUser)

//修改密码
router.post('/pass', verifyjwt, pass)


module.exports = router